/*
 * Main.fx
 *
 * Created on 18-mei-2010, 15:16:17
 */
package syfer;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.control.ListView;
import javafx.scene.control.Button;

import syfer.TextArea;

import sound.Microphone;
import java.net.Socket;
import javafx.scene.control.Label;
import java.lang.Exception;
import network.ReadWriteTCPConnectionToServer;
import javafx.scene.control.TextBox;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.scene.control.Slider;
import sound.UDPStream;

var channel:String = FX.getArgument("channel_id") as String;
var serverip:String = FX.getArgument("server_ip") as String;
var tcpverbinding: Socket;
var server: ReadWriteTCPConnectionToServer;
var mic: Microphone;

try {
    if (channel == null or channel.equals(""))
        channel = "44444";
    if (serverip == null or serverip.equals(""))
        serverip = "10.92.114.110";

     tcpverbinding = new Socket(serverip,55556);
     mic = new Microphone(serverip, Integer.parseInt(channel));

     server = new ReadWriteTCPConnectionToServer(tcpverbinding);
     server.sendData("port:{UDPStream.getLocalPort()}");
     server.sendData("channel:{channel}");
} catch (e: Exception) {
    println("{e.getMessage()}");
}

var connectedPeople: Object[];
var events: String;
//polling tussen java en javafx
var poll: Timeline = Timeline {
            repeatCount: Timeline.INDEFINITE
            keyFrames: [
                KeyFrame {
                    time: 1s
                    action: function () {
                        events = server.history;
                        connectedPeople = server.connectedPeople.toArray();
                        mic.ruiswaarde = 60 + voiceActivationSlider.value as Integer;
                    }
                }
            ]
        }
var inputNickname: TextBox = TextBox {
            text: "nickname"
            translateX: 5
            translateY: 20
            columns: 12
            selectOnFocus: true
        }
var changeNickname: Button = Button {
            text: "Change"
            action: function () {
                server.sendData("nickname:{inputNickname.text.trim()}");
            }
            translateX: 130
            translateY: 20
        }
var lbltitel: Label = Label {
            text: "Connected to channel: {channel}"
            translateX: 5
            translateY: 2
        }
var connectedList: ListView = ListView {
            translateX: 275,
            translateY: 70,
            height: 155,
            width: 100,
            items: bind connectedPeople;
        }
var lblConnected: Text = Text {
            font: Font {
                size: 10
            }
            x: 275
            y: 60
            content: "People Connected:"
        }
var eventsBox: TextArea = TextArea {
            text: bind events
            x: 5
            y: 71
            rows: 10
            cols: 30
        }
var lblEvents: Text = Text {
            font: Font {
                size: 10
            }
            x: 5
            y: 60
            content: "Events:"
        }
var btnActivate: Button = Button {
            text: "Activate",
            translateX: 5,
            translateY: 240,
            action: function () {
                if (btnActivate.text.equals("Activate")) {//aanzetten
                    btnActivate.text = "Disactivate";
                    mic.begin();
                } else {//afzetten
                    btnActivate.text = "Activate";
                    mic.end();
                }
            }
        }
var voiceActivationSlider: Slider = Slider {
            min: 0.0
            max: 67.0
            vertical: false
            translateX: 170
            translateY: 240
            width: 200
            value: 40.0
        }
var voicelabel = Label {
    text: bind "{%(,.0f (voiceActivationSlider.value/67*100)}% noise"
    translateX: 100
    translateY: 240
    width: 80
}

poll.play();

/**
 * Stage with content elements
 */
Stage {
    title: "SYFeR VoIP Chat Client"
    scene: Scene {
        width: 380
        height: 270
        content: [
            lbltitel,
            btnActivate,
            lblConnected,
            connectedList,
            lblEvents,
            inputNickname,
            changeNickname,
            voiceActivationSlider,
            voicelabel,
            eventsBox   
        ]
    }
    onClose: function () {

    }
}
